/********************************************************************

*        Cleaning worker atttitides data from endline               *

********************************************************************/

clear all
set more off
set segmentsize 3g


* Load Data
use "$Data/Original/worker_attitudes_endline.dta", clear


* Baseline IDs
destring bid, gen(_id)


* Egg and Maida are common sections that serve all lines
replace Section = Line if inlist(Line, "Egg", "Maida")

** More comfortable communicating with coreligionists **
assert !mi(attitude_communication_religion)
gen comm_rel = attitude_communication_religion == "Always"
replace comm_rel = 0.5 if attitude_communication_religion == "Sometimes"

* Recoding communication variables to make sign consistent with other outcomes
local varlist comm_rel
foreach var of local varlist{
replace `var' = 2 if `var' == 1
replace `var' = 1 if `var' == 0
replace `var' = 0 if `var' == 2
}

/** Comfortable taking orders from non-coreligionists **/
assert !mi(attitude_orders_religion)
gen orders_rel = (attitude_orders_religion == "Always") /* 0 if  Sometimes or Never comfortable */

/** Preference for working with non-coreligionists **/
assert !mi(attitude_workpref_religion)
gen workp = (attitude_workpref_religion == "Never") // Strict preference against homogeneous team
replace workp = 0.5 if attitude_workpref_religion == "Sometimes" // Sometimes prefer homogeneous team

* Encode categorical variables
encode Line, gen(line)
encode Section, gen(section)
egen line_s = group(line section)
egen line_sec_team = group(line section Team)

* Merge with baseline data (IDs entered wrongly or missing were directly corrected on the endline forms)
merge 1:1 _id using "$Data/Final/Baseline_Cleaned.dta", keep(3) nogen
disp _N // 546

* Handle baseline controls that have missing values
foreach var of varlist comm_relb-orders_relb {
    gen `var'_missing = missing(`var')
    replace `var' = 99 if missing(`var')
}

* Get numeric IAT score
destring IAT_score, replace

/** Create standardized index for attitudes **/
foreach var of varlist comm_rel orders_rel workp {
    quietly summarize `var' if mixed == 0, detail // Reference group: Hindus in homogeneous teams
    gen `var'_std = (`var' - r(mean)) / r(sd)
}
egen attitudes_index = rowmean(comm_rel_std orders_rel_std workp_std)
label variable attitudes_index "Mean of three standardized measures of attitudes"


*-------------------------------*
* Assign Treatment Status -----*
*-------------------------------*
/*

This part is commented out since it had identifying info because production lines are named by product brands. Line names have been replace by numbers here. 

g mixed = 0
g dep = 0

        * Line 3
        replace dep = 1 if inlist(Section, "1St Line", "2Nd Line", "Cfc", "Depanning", "Packing") & Line == "Line 3"
        replace mixed = 1 if inlist(Section, "1St Line", "2Nd Line", "Cfc", "Depanning", "Packing") & inlist(Team, "X") & Line == "Line 3"
		replace mixed = 1 if !inlist(Section, "1St Line", "2Nd Line", "Cfc", "Depanning", "Packing")  & inlist(Team, "Y", "Z") & Line == "Line 3"

        * Line 1
		replace dep = 1 if inlist(Section, "Cfc", "Deposit", "Packing") & Line == "Line 1"
        replace mixed = 1 if inlist(Section, "Cfc", "Deposit", "Packing")  & inlist(Team, "X", "Y") & Line == "Line 1"
        replace mixed = 1 if !inlist(Section, "Cfc", "Deposit", "Packing") & inlist(Team, "Z") & Line == "Line 1"
        
		* Line 2
        replace dep = 1 if inlist(Section, "Cfc", "Deposit", "Packing", "Inject") & Line == "Line 2"
        replace mixed = 1 if inlist(Section, "Cfc", "Deposit", "Packing", "Inject")  & inlist(Team,  "Y") & Line == "Line 2"
        replace mixed = 1 if !inlist(Section, "Cfc", "Deposit", "Packing", "Inject") &  inlist(Team,  "X", "Z") & Line == "Line 2"
    
	    * Line 6
		replace dep = 1 if inlist(Section, "Box Filling", "Cfc") & Line == "Line 6"
        replace mixed = 1 if !inlist(Section, "Box Filling", "Cfc") & Line == "Line 6"
		
		* Line 5
    	replace dep = 1 if inlist(Section, "Box Filling", "Box Machine", "Cfc") & Line == "Line 5"
        replace mixed = 1 if inlist(Section, "Box Filling", "Box Machine", "Cfc") & inlist(Team, "X", "Z") & Line == "Line 5"
        replace mixed = 1 if !inlist(Section, "Box Filling", "Cfc") & inlist(Team, "Y") & Line == "Line 5"
    
	    * Line 4
        replace dep = 1 if inlist(Section, "Packing", "Cfc")  & Line == "Line 4"
        replace mixed = 1 if inlist(Section, "Packing", "Cfc") &  inlist(Team, "Y") & Line == "Line 4"
        replace mixed = 1 if !inlist(Section, "Packing", "Cfc")  & inlist(Team, "X") & Line == "Line 4"
 
        * Maida
        replace dep = 1 if Line == "Maida"
        replace mixed = 1 if inlist(Team, "Y", "Z") & Line == "Maida"
 
        * Egg
        replace dep = 0 if Line == "Egg"
        replace mixed = 1 if inlist(Team, "X", "Y") & Line == "Egg"
 
*/


** HD vs LD dummies for sections **
gen LD = (dep == 0)
gen HD = (dep == 1) 

* Interaction terms for treatment effects
gen mixedXLD = mixed * LD
gen mixedXHD = mixed * HD

* Label outcome variables
la var comm_rel "Comfortable communicating with non-coreligionists"
la var orders_rel "Comfortable taking orders from non-coreligionists"
la var workp "Comfortable co-working with non-coreligionists"

* Save cleaned and processed data
save "$Data/Final/worker_attitudes.dta", replace
